<?php
class NotifyAction extends Action {
	public function getFeed($feed_id){
		//$feed_id = $_REQUEST['feed_id'];
		$data = model("Feed")->getFeedInfo($feed_id);
		//dump($data);
		return $data;
	}
	
	public function getNotify(){//【获取消息列表】
		$uid = $_REQUEST['uid'];
		$type = $_REQUEST['type'];//类型，必填
		$pagecount=isset($_POST['"pagecount'])?$_POST['pagecount']:"10";//每页显示数量
		$pagenum=$pagecount*((isset($_POST['pagenum'])?$_POST['pagenum']:"1")-1);//当前页数
		if($type == 'comment'){//获取评论
			$comment = M('Comment');
			$data = $comment->where("app_uid = $uid or to_uid = $uid")->order("ctime desc")->limit("$pagenum,$pagecount")->select();
			if($data){
				foreach ($data as $key=>$da){
						$data[$key]['feed_id'] = $da['row_id'];
						$info = M('User')->where('uid = '.$da['uid'])->find();
						$data[$key]['from_avatar'] = $info['avatar'];
						$data[$key]['from_uname'] = $info['uname'];
						$data[$key]['content'] = $da['content'];
						if($da['to_uid']){
							$info = M('User')->where('uid = '.$da['to_uid'])->find();
							$data[$key]['to_uname'] = $info['uname'];
							$data[$key]['type'] = '2';//回复
						}else{
							$data[$key]['to_uname'] = '';
							$data[$key]['type'] = '1';//评论
						}
						//获取动态内容
						$feed_data = $this->getFeed($da['row_id']);
						$data[$key]['feed_content'] = $feed_data['content'];
						if(empty( $feed_data['attach'])){
							$data[$key]['attach'] = array();
						}else{
							$data[$key]['attach'] = $feed_data['attach'];
						
						}
				}
			}
			
		}elseif($type == 'like'){//获取点赞
			$like = M('Feed_like');
			$data = $like->where("app_uid = $uid")->order("ctime desc")->limit("$pagenum,$pagecount")->select();
			foreach ($data as $key=>$da){
				$info = M('User')->where('uid = '.$da['uid'])->find();
				$data[$key]['feed_id'] = $da['feed_id'];
				$data[$key]['from_avatar'] = $info['avatar'];
				$data[$key]['from_uname'] = $info['uname'];
				$feed_data = $this->getFeed($da['feed_id']);
				$data[$key]['feed_content'] = $feed_data['content'];
				if(empty( $feed_data['attach'])){
							$data[$key]['attach'] = array();
						}else{
							$data[$key]['attach'] = $feed_data['attach'];
						
						}
			}
		}
		
		if($data){
			$value = ['code'=>'success','data'=>$data];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}else{
			if(is_array($data) || mysql_num_rows($data) < 1){
				$value = ['code'=>'success','data'=>array()];
				header("Content-Type: text/json");
    			die(json_encode($value));
			}else{
				$value = ['code'=>'false'];
				header("Content-Type: text/json");
    			die(json_encode($value));
			}
		}
	}
	
	public function checkNotify(){//【检查是否有新的消息】
		$uid = $_REQUEST['uid'];
		$type = $_REQUEST['type'];//类型，可选
		$user_notify = M("User_notify");
		
		$find_count = $user_notify->where("uid = $uid")->find();
		
		if($type == 'comment' && $find_count['comment']){
			$value = ['code'=>'havenew','count'=>$find_count['comment']];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
		if($type == 'like' && $find_count['like']){
			$value = ['code'=>'havenew','count'=>$find_count['like']];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
		if($type == '' && ($find_count['comment'] || $find_count['like'])){
			$value = ['code'=>'havenew','count'=>$find_count['comment']+$find_count['like']];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
			$value = ['code'=>'nohave'];
			header("Content-Type: text/json");
    		die(json_encode($value));
	}
	
	public function addNotify($uid,$type){//【添加通知】
//		$uid = $_REQUEST['uid'];
//		$type = $_REQUEST['type'];
		$user_notify = M("User_notify");
		$find = $user_notify->where("uid = $uid")->find();
		if(!$find){
			$user_notify->uid = $uid;
			$user_notify->add();
		}
		$info = $user_notify->where("uid = $uid")->find();
		
		$like = $info['like']+1;
		$comment = $info['comment']+1;
		
		if($type == 'comment'){//评论
			$user_notify->uid = $uid;
			$user_notify->comment = $comment;
		}else{//回复
			$user_notify->uid = $uid;
			$user_notify->like = $like;
		}
		//dump($user_notify);
		$user_notify->where("uid = $uid")->save();
	}
	
	public function breakNotify(){//【全部为已读】
		$uid = $_REQUEST['uid'];
		$type = $_REQUEST['type'];
		$user_notify = M("User_notify");
		
		if($type == 'comment'){
			$data->comment = 0;
			$update = $user_notify->where("uid = $uid")->data($data)->save();
		}
		if($type == 'like'){
			$data->like = 0;
			$update = $user_notify->where("uid = $uid")->data($data)->save();
		}
		if($update){
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
		if(!mysql_error()){
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
    		die(json_encode($value));
	}
	
	public function getFeedData(){//【获取单个动态】
		$feed_id = $_REQUEST['feed_id'];
		$data = $this->getFeed($feed_id);
		$uid = $data['uid'];
		$avatar = M('User')->where("uid = $uid")->field('avatar')->find();
		$data['avatar'] = $avatar['avatar'];
		$feed = new FeedAction();
		$allComment = $feed->getAllcomment($feed_id);
		if($allComment!=null){
			foreach ($allComment as $key=>$c){
				if($c['to_uid']){
					$to_uid = $c['to_uid'];
					$user = M('User')->where("uid = $to_uid")->field("uname")->find();
					$allComment[$key]['to_uname'] = $user['uname'];
				}
						
			}
		}
		$data['comment'] = $allComment;
		$data['like'] = $feed->getLike($feed_id);
		unset($data['feed_content']);
		unset($data['avatar_big']);
		unset($data['avatar_middle']);
		unset($data['avatar_small']);
		unset($data['client_ip']);
		unset($data['comment_all_count']);
		unset($data['is_del']);
		unset($data['app_row_table']);
		unset($data['app']);
		unset($data['source_body']);
		$result = array();
		$result[0]['feed_data'] = $data;
		if(!empty($data)){
			$value = ['code'=>'success','data'=>$result];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
	}
	
}